#ifndef LIST_H
#define LIST_H

#include<stddef.h>
/*
 *  * Simple doubly linked list implementation.
 *   *
 *    * Some of the internal functions ("__xxx") are useful when
 *     * manipulating whole lists rather than single entries, as
 *      * sometimes we already know the next/prev entries and we can
 *       * generate better code by using them directly rather than
 *        * using the generic single-entry routines.
 *         */

struct list_head {
	    struct list_head *next, *prev;
};

#define LIST_HEAD_INIT(name) { &(name), &(name) }

#define LIST_HEAD(name) \
	    struct list_head name = LIST_HEAD_INIT(name)

static inline void INIT_LIST_HEAD(struct list_head *list)
{
	    list->next = list;
	    list->prev = list;
}

/*
 *  * Insert a new entry between two known consecutive entries.
 *   *
 *    * This is only for internal list manipulation where we know
 *     * the prev/next entries already!
 *      */
static inline void __list_add(struct list_head *new_node,
		                  struct list_head *prev,
				                    struct list_head *next)
{
	    next->prev = new_node;
	    new_node->next = next;
	    new_node->prev = prev;
            prev->next = new_node;
}

/**
 *  * list_add - add a new entry
 *   * @new: new entry to be added
 *    * @head: list head to add it after
 *     *
 *      * Insert a new entry after the specified head.
 *       * This is good for implementing stacks.
 *        */
static inline void list_add(struct list_head *new_node, struct list_head *head)
{
	    __list_add(new_node, head, head->next);
}


/**
 *  * list_add_tail - add a new entry
 *   * @new: new entry to be added
 *    * @head: list head to add it before
 *     *
 *      * Insert a new entry before the specified head.
 *       * This is useful for implementing queues.
 *        */
static inline void list_add_tail(struct list_head *new_node, struct list_head *head)
{
	    __list_add(new_node, head->prev, head);
}

/*
 *  * Delete a list entry by making the prev/next entries
 *   * point to each other.
 *    *
 *     * This is only for internal list manipulation where we know
 *      * the prev/next entries already!
 *       */
static inline void __list_del(struct list_head * prev, struct list_head * next)
{
	    next->prev = prev;
	    prev->next = next;
}

/**
 *  * list_del - deletes entry from list.
 *   * @entry: the element to delete from the list.
 *    * Note: list_empty() on entry does not return true after this, the entry is
 *     * in an undefined state.
 *      */
static inline void list_del(struct list_head *entry)
{
	    __list_del(entry->prev, entry->next);
}

/**
 *  * list_replace - replace old entry by new one
 *   * @old : the element to be replaced
 *    * @new : the new element to insert
 *     *
 *      * If @old was empty, it will be overwritten.
 *       */
static inline void list_replace(struct list_head *old,
		                struct list_head *new_node)
{
	    new_node->next = old->next;
	    new_node->next->prev = new_node;
	    new_node->prev = old->prev;
	    new_node->prev->next = new_node;
}

/**
 *  * list_move - delete from one list and add as another's head
 *   * @list: the entry to move
 *    * @head: the head that will precede our entry
 *     */
static inline void list_move(struct list_head *list, struct list_head *head)
{
	    __list_del(list->prev, list->next);
	    list_add(list, head);
}

/**
 *  * list_move_tail - delete from one list and add as another's tail
 *   * @list: the entry to move
 *    * @head: the head that will follow our entry
 *     */
static inline void list_move_tail(struct list_head *list,
		                  struct list_head *head)
{
	    __list_del(list->prev, list->next);
	    list_add_tail(list, head);
}

/**
 *  * list_is_last - tests whether @list is the last entry in list @head
 *   * @list: the entry to test
 *    * @head: the head of the list
 *     */
static inline int list_is_last(const struct list_head *list,
		                const struct list_head *head)
{
	    return list->next == head;
}

/**
 *  * list_empty - tests whether a list is empty
 *   * @head: the list to test.
 *    */
static inline int list_empty(const struct list_head *head)
{
	    return head->next == head;
}

/**
 *  * list_is_singular - tests whether a list has just one entry.
 *   * @head: the list to test.
 *    */
static inline int list_is_singular(const struct list_head *head)
{
	    return !list_empty(head) && (head->next == head->prev);
}



/**
 *  * list_entry - get the struct for this entry
 *   * @ptr:    the &struct list_head pointer.
 *    * @type:    the type of the struct this is embedded in.
 *     * @member:    the name of the list_struct within the struct.
 *      */
#define list_entry(ptr, type, member) \
	    container_of(ptr, type, member)

/**
 *  * list_for_each    -    iterate over a list
 *   * @pos:    the &struct list_head to use as a loop cursor.
 *    * @head:    the head for your list.
 *     *
 *      * This variant differs from list_for_each() in that it's the
 *       * simplest possible list iteration code, no prefetching is done.
 *        * Use this for code that knows the list to be very short (empty
 *         * or 1 entry) most of the time.
 *          */
#define list_for_each(pos, head) \
	    for (pos = (head)->next; pos != (head); pos = pos->next)

#define container_of(ptr, type, member) ({                    \
		        const typeof(((type *)0)->member)*__mptr = (ptr);    \
		        (type *)((char *)__mptr - offsetof(type, member)); })


/*
 * Double linked lists with a single pointer list head.
 * Mostly useful for hash tables where the two pointer list head is
 * too wasteful.
 * You lose the ability to access the tail in O(1).
 */

struct hlist_head {
	struct hlist_node *first;
};

struct hlist_node {
	struct hlist_node *next, **pprev;
};

#define HLIST_HEAD_INIT { .first = NULL }
#define HLIST_HEAD(name) struct hlist_head name = {  .first = NULL }
#define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
static inline void INIT_HLIST_NODE(struct hlist_node *h)
{
	h->next = NULL;
	h->pprev = NULL;
}

static inline int hlist_unhashed(const struct hlist_node *h)
{
	return !h->pprev;
}

static inline int hlist_empty(const struct hlist_head *h)
{
	return !h->first;
}

static inline void __hlist_del(struct hlist_node *n)
{
	struct hlist_node *next = n->next;
	struct hlist_node **pprev = n->pprev;
	*pprev = next;
	if (next)
		next->pprev = pprev;
}

static inline void hlist_del(struct hlist_node *n)
{
	__hlist_del(n);
	n->next = NULL;
	n->pprev = NULL;
}

static inline void hlist_del_init(struct hlist_node *n)
{
	if (!hlist_unhashed(n)) {
		__hlist_del(n);
		INIT_HLIST_NODE(n);
	}
}

static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
{
	struct hlist_node *first = h->first;
	n->next = first;
	if (first)
		first->pprev = &n->next;
	h->first = n;
	n->pprev = &h->first;
}

/* next must be != NULL */
static inline void hlist_add_before(struct hlist_node *n,
					struct hlist_node *next)
{
	n->pprev = next->pprev;
	n->next = next;
	next->pprev = &n->next;
	*(n->pprev) = n;
}

static inline void hlist_add_after(struct hlist_node *n,
					struct hlist_node *next)
{
	next->next = n->next;
	n->next = next;
	next->pprev = &n->next;

	if(next->next)
		next->next->pprev  = &next->next;
}

/* after that we'll appear to be on some hlist and hlist_del will work */
static inline void hlist_add_fake(struct hlist_node *n)
{
	n->pprev = &n->next;
}

/*
 * Move a list from one list head to another. Fixup the pprev
 * reference of the first entry if it exists.
 */
static inline void hlist_move_list(struct hlist_head *old,
				   struct hlist_head *new_node)
{
	new_node->first = old->first;
	if (new_node->first)
		new_node->first->pprev = &new_node->first;
	old->first = NULL;
}

#define hlist_entry(ptr, type, member) container_of(ptr,type,member)

#define hlist_for_each(pos, head) \
	for (pos = (head)->first; pos ; pos = pos->next)

#define hlist_for_each_safe(pos, n, head) \
	for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
	     pos = n)

/**
 * hlist_for_each_entry	- iterate over list of given type
 * @tpos:	the type * to use as a loop cursor.
 * @pos:	the &struct hlist_node to use as a loop cursor.
 * @head:	the head for your list.
 * @member:	the name of the hlist_node within the struct.
 */
#define hlist_for_each_entry(tpos, pos, head, member)			 \
	for (pos = (head)->first;					 \
	     pos &&							 \
		({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
	     pos = pos->next)

/**
 * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
 * @tpos:	the type * to use as a loop cursor.
 * @pos:	the &struct hlist_node to use as a loop cursor.
 * @member:	the name of the hlist_node within the struct.
 */
#define hlist_for_each_entry_continue(tpos, pos, member)		 \
	for (pos = (pos)->next;						 \
	     pos &&							 \
		({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
	     pos = pos->next)

/**
 * hlist_for_each_entry_from - iterate over a hlist continuing from current point
 * @tpos:	the type * to use as a loop cursor.
 * @pos:	the &struct hlist_node to use as a loop cursor.
 * @member:	the name of the hlist_node within the struct.
 */
#define hlist_for_each_entry_from(tpos, pos, member)			 \
	for (; pos &&							 \
		({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
	     pos = pos->next)

/**
 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
 * @tpos:	the type * to use as a loop cursor.
 * @pos:	the &struct hlist_node to use as a loop cursor.
 * @n:		another &struct hlist_node to use as temporary storage
 * @head:	the head for your list.
 * @member:	the name of the hlist_node within the struct.
 */
#define hlist_for_each_entry_safe(tpos, pos, n, head, member) 		 \
	for (pos = (head)->first;					 \
	     pos && ({ n = pos->next; 1; }) && 				 \
		({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
	     pos = n)


/* 2^31 + 2^29 - 2^25 + 2^22 - 2^19 - 2^16 + 1 */
#define GOLDEN_RATIO_PRIME_32 0x9e370001UL

static inline long unsigned hash_32(void * val, unsigned int bits)
{
	/* On some cpus multiply is faster, on others gcc will do shifts */
	long unsigned  hash = (long unsigned)val * GOLDEN_RATIO_PRIME_32;

	/* High bits are more random, so use them. */
	return hash >> (32 - bits);
}

/*  2^63 + 2^61 - 2^57 + 2^54 - 2^51 - 2^18 + 1 */
#define GOLDEN_RATIO_PRIME_64 0x9e37fffffffc0001UL
static inline unsigned long hash_64(unsigned long val, unsigned int bits)
{
	unsigned long hash = val;

	/*  Sigh, gcc can't optimise this alone like it does for 32 bits. */
	unsigned long n = hash;
	n <<= 18;
	hash -= n;
	n <<= 33;
	hash -= n;
	n <<= 3;
	hash += n;
	n <<= 3;
	hash -= n;
	n <<= 4;
	hash += n;
	n <<= 2;
	hash += n;

	/* High bits are more random, so use them. */
	return hash >> (64 - bits);
}

static inline unsigned long hash_ptr(const void *ptr, unsigned int bits)
{
	return hash_64((unsigned long)ptr, bits);
}

#endif
